// request.js
// 通常可以吧 baseUrl 单独放在一个 js 文件了
const baseUrl = uni.getStorageSync("localurl"); 

 const request = (options = {}) => {
 // 在这里可以对请求头进行一些设置
 let userInfo = uni.getStorageSync("userInfo");
 if (userInfo) {
 	options.data['userLevelCode'] =JSON.parse(userInfo).levelCode;
 	options.data['loginUserId'] = JSON.parse(userInfo).id;
 }
 let userDept = uni.getStorageSync("userDept");
 console.log(userDept);
 if (userDept) {
 	options.data['loginUserDeptLevelCode'] = userDept.levelCode;
 	options.data['loginUserDeptId'] = userDept.id;
 }
 let currentRole = uni.getStorageSync("userRoles");
  console.log(currentRole);
 if (currentRole) {
 	options.data['roleLevelCode'] = currentRole[0].levelCode;
 	options.data['roleId'] = currentRole[0].id;
 }
  options.header = {
      'Content-Type':'application/json',
      Authorization: uni.getStorageSync("authorization")
 }

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + options.url || '',
            method: options.type || 'POST',
            data: options.data || {},
            header: options.header || {}      
        }).then(data => {
            let [err, res] = data;        
            resolve(res);
        }).catch(error => {
            reject(error)
        })
    });
}

 const get = (url, data, options = {}) => {
    
    options.type = 'GET';
    options.data = data;
    options.url = url;
    return request(options)
}

const post = (url, data, options = {}) => {
    options.type = 'POST';
    options.data = data;
    options.url = url;
    return request(options)
}


export default  {
    request,
    get,
    post
}